package org.example.controller;


import org.example.controller.request.LinkGroupAddRequest;
import org.example.controller.request.LinkGroupUpdateRequest;
import org.example.enums.BizCodeEnum;
import org.example.service.LinkGroupService;
import org.example.util.JsonData;
import org.example.vo.LinkGroupVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 谢超
 * @since 2023-08-15
 */
@RestController
@RequestMapping("/api/group/v1")
public class LinkGroupController {

    @Resource
    private LinkGroupService linkGroupService;

    /**
     * 用户创建分组
     * @param addRequest
     * @return
     */
    @PostMapping("add")
    public JsonData addGroup (@RequestBody LinkGroupAddRequest addRequest) {
        int row = linkGroupService.addGroup(addRequest);
        return row == 1 ? JsonData.buildSuccess() : JsonData.buildResult(BizCodeEnum.GROUP_ADD_FAILED);
    }

    /**
     * 用户根据id删除分组
     * @param groupId
     * @return
     */
    @DeleteMapping("/del/{group_id}")
    public JsonData delGroup (@PathVariable("group_id") long groupId) {
        int row = linkGroupService.delGroup(groupId);
        return row == 1 ? JsonData.buildSuccess() : JsonData.buildResult(BizCodeEnum.GROUP_OPER_FAIL);
    }

    /**
     * 用户根据id查询详情数据
     * @param groupId
     * @return
     */
    @GetMapping("/getGroupDetails/{group_id}")
    public JsonData getGroupDetails(@PathVariable("group_id") long groupId) {
        LinkGroupVO linkGroupVO = linkGroupService.getGroupDetails(groupId);
        return JsonData.buildSuccess(linkGroupVO);
    }

    /**
     * 查询用户全部分组
     * @return
     */
    @GetMapping("list")
    public JsonData getAllGroup () {
        List<LinkGroupVO> groupList = linkGroupService.getAllGroup();
        return JsonData.buildSuccess(groupList);
    }

    @PutMapping("update")
    public JsonData updateGroup (@RequestBody LinkGroupUpdateRequest updateRequest) {
        int row = linkGroupService.updateGroupById(updateRequest);
        return row == 1 ? JsonData.buildResult(BizCodeEnum.OPTIONS_UPDATE) : JsonData.buildResult(BizCodeEnum.GROUP_OPER_FAIL);
    }
}

